<?php

require_once dirname(__FILE__) . '/../lib/bb_reserva_libroGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/bb_reserva_libroGeneratorHelper.class.php';

/**
 * bb_reserva_libro actions.
 *
 * @package    ingenyo-biblio
 * @subpackage bb_reserva_libro
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class bb_reserva_libroActions extends autoBb_reserva_libroActions
{

  public function executeNew(sfWebRequest $request)
  {
    $id_documento = $this->getUser()->getAttribute('documento');
    $this->form = new BbReservaLibroForm(null, array('idDocumento' => $id_documento));
    $this->BbReservaLibro = $this->form->getObject();
    $this->setLayout('layout_iframe');

  }

  public function executeCreate(sfWebRequest $request)
  {
    $id_documento = $this->getUser()->getAttribute('documento');
    $this->form = new BbReservaLibroForm(null, array('idDocumento' => $id_documento));
    $this->processForm($request, $this->form);
    $this->BbReservaLibro = $this->form->getObject();
    $this->setTemplate('new');
    $this->setLayout('layout_iframe');

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->nuevo = $this->getUser()->hasAttribute('nuevo');
    $this->getUser()->clearAttribute('nuevo');
    parent::executeShow($request);
    $this->setLayout('layout_iframe');

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'La reserva se ha realizado con exito.' : 'La reserva a sido actualizada.';

      $reserva = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $reserva)));

      $this->getUser()->setFlash('notice', $notice, false);
      $persona = $reserva->getPersona();
      #$persona->sendMailCreacionReserva($reserva);
      $this->getUser()->setAttribute('nuevo', true);
      $this->redirect(array('sf_route' => 'bb_reserva_libro_show', 'sf_subject' => $reserva));
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }

  }

  public function executeRetirar(sfWebRequest $request)
  {
    $reserva = $this->getRoute()->getObject();
    if ($reserva->getFechacomienzo() == null)
    {
      $this->getUser()->setFlash('error', 'El libro no puede ser retirado porque se encuentra prestado o tiene reservas con mayor prioridad.', true);
      $this->redirect('bb_reserva_libro_show', $reserva);
    }
    elseif ($reserva->getLibro()->getEstadolibro() == ReferenceEstadoLibro::PRESTADO)
    {
      $this->getUser()->setFlash('error', 'El libro no puede ser retirado debido a que se encuentra prestado.', true);
      $this->redirect('bb_reserva_libro_show', $reserva);
    }
    else
    {
      $this->BbPrestamo = $reserva->retirar();
      $this->getUser()->setAttribute('BbPrestamo', $this->BbPrestamo);
      $this->redirect('bb_prestamo/newPrestamoDeReserva');
    }

  }

  public function executeCancelar(sfWebRequest $request)
  {
    $this->reserva = $this->getRoute()->getObject();
    $this->reserva->cancelar();
    $this->getUser()->setFlash('notice', 'La reserva se cancelo y el libro paso a estado activo.', false);
    
    $this->setTemplate('cancelar');

    $this->setLayout('layout_iframe');



  }

  public function executeIndexByPersona(sfWebRequest $request)
  {
    parent::executeIndex($request);
    $this->getUser()->setAttribute('action', 'indexByPersona');
    $this->pager->setCriteria(BbreservalibroPeer::getVigentesByPersonaCriteria($this->getUser()->getAttribute('id_persona')));
    $this->pager->init();
    $this->setTemplate('index');
    $this->setLayout('layout_iframe');

  }

  public function executeIndexByDocumento(sfWebRequest $request)
  {
    parent::executeIndex($request);
    $this->getUser()->setAttribute('action', 'indexByDocumento');
    $this->pager->setCriteria(BbreservalibroPeer::getVigentesByDocumentoCriteria($this->getUser()->getAttribute('id_documento')));
    $this->pager->init();
    $this->setTemplate('index');
    $this->setLayout('layout_iframe');

  }

}
